using System;
using System.Collections.Generic;
using System.Linq;
using YTS.Tools;

namespace Test.ConsoleProgram.Algorithm.Work
{
    [TestDescription("工作逻辑: 处理商城运营目标任务数据内容")]
    public class Test_OptimizationTargetValEditModel : AbsBaseTestItem
    {
        public class Mall_TargetVal
        {
            public Nullable<bool> IsYear { get; set; }
            public Nullable<decimal> TargetValue { get; set; }
            public string Month { get; set; }
            public string MonthValue { get; set; }
        }
        public override void OnTest()
        {
            OnExecute(Optimization);
            OnExecute(CalcAvgMonthValues);
        }
        private void OnExecute(Func<Mall_TargetVal, Mall_TargetVal> func)
        {
            Assert.TestExe(CalcAvgMonthValues,
                new Mall_TargetVal()
                {
                    IsYear = false,
                    TargetValue = null,
                    Month = "1,2,3,4,5,6,7,8,9,10,11,12",
                    MonthValue = "11111,34,53,53,0,34,234,23,235,235,2523,231412",
                },
                new Mall_TargetVal()
                {
                    IsYear = false,
                    TargetValue = null,
                    Month = "1,2,3,4,5,6,7,8,9,10,11,12",
                    MonthValue = "11111,34,53,53,0,34,234,23,235,235,2523,231412",
                });
            Assert.TestExe(CalcAvgMonthValues,
                new Mall_TargetVal()
                {
                    IsYear = true,
                    TargetValue = 2222,
                    Month = "1,2,3,4,5,6,7,8,9,10,11,12",
                    MonthValue = "11111,34,53,53,0,34,234,23,235,235,2523,231412",
                },
                new Mall_TargetVal()
                {
                    IsYear = true,
                    TargetValue = 2222,
                    Month = "1,2,3,4,5,6,7,8,9,10,11,12",
                    MonthValue = "185,185,185,185,185,185,185,185,185,185,185,187",
                });
        }

        private Mall_TargetVal Optimization(Mall_TargetVal model)
        {
            const int MonthCount = 12;
            var arrMonthValue = (model.MonthValue ?? "").Split(',');
            int yearValueAvg = Convert.ToInt32((model.TargetValue ?? 0) / MonthCount);
            yearValueAvg = (model.IsYear ?? false) ? yearValueAvg : -1;
            int yearValueRemainder = Convert.ToInt32((model.TargetValue ?? 0) % MonthCount);
            if (yearValueAvg > -1)
            {
                for (var i = 0; i < arrMonthValue.Length; i++)
                {
                    arrMonthValue[i] = yearValueAvg.ToString();
                    if (i == arrMonthValue.Length - 1)
                    {
                        arrMonthValue[i] = (yearValueAvg + yearValueRemainder).ToString();
                    }
                }
            }
            model.MonthValue = string.Join(',', arrMonthValue);
            return model;
        }

        public class MonthShow
        {
            public int Month { get; set; }
            public decimal Value { get; set; }

            public static IList<MonthShow> ToMonthShow(Mall_TargetVal model)
            {
                var Months = (model.Month ?? "").Split(',') ?? new string[] { };
                var Values = (model.MonthValue ?? "").Split(',') ?? new string[] { };
                var rlist = new List<MonthShow>();
                for (int i = 0; i < Months.Length; i++)
                {
                    rlist.Add(new MonthShow()
                    {
                        Month = ConvertTool.ToInt(Months.GetIndexValue(i, null), 0),
                        Value = ConvertTool.ToDecimal(Values.GetIndexValue(i, null), 0),
                    });
                }
                return rlist;
            }
        }
        private Mall_TargetVal CalcAvgMonthValues(Mall_TargetVal model)
        {
            IList<MonthShow> months = MonthShow.ToMonthShow(model)
                .OrderBy(m => m.Month)
                .ToArray();
            if (model.IsYear ?? false)
            {
                var yearValue = model.TargetValue ?? 0;
                var yearValueAvg = ConvertTool.ToFixed(yearValue / months.Count, 0);
                var yearValueRemainder = ConvertTool.ToFixed(yearValue % months.Count, 0);
                var n = months.Count;
                for (int i = 0; i < n; i++)
                {
                    months[i].Value = yearValueAvg;
                    if (i == n - 1)
                    {
                        months[i].Value += yearValueRemainder;
                    }
                }
            }
            var keys = months.Select(m => m.Month).ToArray();
            var values = months.Select(m => m.Value).ToArray();
            model.Month = string.Join(",", keys);
            model.MonthValue = string.Join(",", values);
            return model;
        }
    }

    public static class ConvertToolExtend
    {
        /// <summary>
        /// 获取列表索引值
        /// </summary>
        /// <typeparam name="T">列表数据类型</typeparam>
        /// <param name="list">列表集合</param>
        /// <param name="index">索引</param>
        /// <param name="defalutValue">默认值</param>
        /// <returns>结果</returns>
        public static T GetIndexValue<T>(this IList<T> list, int index, T defalutValue = default(T))
        {
            if (index < 0 || index >= list.Count)
                return defalutValue;
            return list[index];
        }
    }
}
